************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 1 (Balance Across Each Experimental Treatment Cell (6 arms -- 3 sleep possibilities times 2 nap possibilities))
* Last edited: 07 May 2021
************************************************
	
	clear all
	set more off
	set matsize 800
		
	use "$d/main_index_dataset.dta", clear
	
	keep pid wellbeing_index_base cogindex_pre pref_pre
	collapse wellbeing_index_base cogindex_pre pref_pre, by (pid)
	
	tempfile index
	save "`index'"
	 
use "$d/balance_dataset.dta", clear	

	merge m:1 pid using "`index'"
	keep if _merge==3
	
	rename wb_bsl wb_bsl_old
	rename wellbeing_index_base wb_bsl 
	rename cogindex_pre cg_bsl
	rename pref_pre pref_bsl
	

	cap egen tag = tag(pid) if post_treatment == 1
	
	* Generating the new treatments
	
	gen treat_s_nonap = (treat_s == 1 & treat_nap == 0)
	gen treat_si_nonap = (treat_s_i == 1 & treat_nap == 0)
	gen treat_s_nap = (treat_s == 1 & treat_nap == 1)
	gen treat_si_nap = (treat_s_i == 1 & treat_nap == 1)
	gen treat_c_nap = (treat_pool == 0 & treat_nap == 1)		
	
local varlist "female age no_of_children education computer_comfort unemployed bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned health_bsl wb_bsl cg_bsl pref_bsl baseline_pvt baseline_hf baseline_corsi baseline_typing baseline_office baseline_prod baseline_earnings bsl_attendance bsl_attendance_2 baseline_savings prior_savings_1000"

foreach var of local varlist {

************ SLEEP TREATMENTS **************************************************

* Sleep control + no nap

	sum `var' if treatment_group==0 & tag==1 & treat_nap==0
	local mean_`var'_cc = string(r(mean), "%3.2f")
	local se_`var'_cc = string(r(sd)/sqrt(r(N)), "%3.2f")

* Encouragement + no nap

	sum `var' if treatment_group==1 & tag==1 & treat_nap==0
	local mean_`var'_dc = string(r(mean), "%3.2f")
	local se_`var'_dc = string(r(sd)/sqrt(r(N)), "%3.2f")

* Incentives + no nap

	sum `var' if treatment_group==2 & tag==1 & treat_nap==0
	local mean_`var'_ic = string(r(mean), "%3.2f")
	local se_`var'_ic = string(r(sd)/sqrt(r(N)), "%3.2f")
	
* Sleep + nap

	sum `var' if treatment_group==0 & tag==1 & treat_nap==1
	local mean_`var'_cn = string(r(mean), "%3.2f")
	local se_`var'_cn = string(r(sd)/sqrt(r(N)), "%3.2f")

* Encouragement + nap

	sum `var' if treatment_group==1 & tag==1 & treat_nap==1
	local mean_`var'_dn = string(r(mean), "%3.2f")
	local se_`var'_dn = string(r(sd)/sqrt(r(N)), "%3.2f")

* Incentives + nap

	sum `var' if treatment_group==2 & tag==1 & treat_nap==1
	local mean_`var'_in = string(r(mean), "%3.2f")
	local se_`var'_in = string(r(sd)/sqrt(r(N)), "%3.2f")
	

* P-values of differences

	reg `var' treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap if tag==1 //P-values for coefficients are p-values for mean-differences!

	lincom _b[treat_s_nonap]
		local pval_`var'_dc = string(r(p), "%3.2f")
	
	lincom _b[treat_si_nonap]
		local pval_`var'_ic = string(r(p), "%3.2f")
		
	lincom _b[treat_c_nap]
		local pval_`var'_cn = string(r(p), "%3.2f")
		
	lincom _b[treat_s_nap]
		local pval_`var'_dn = string(r(p), "%3.2f")
		
	lincom _b[treat_si_nap]
		local pval_`var'_in = string(r(p), "%3.2f")


}

* Counting obs

	count if treatment_group==0 & treat_nap==0 & tag==1
	local N_cc = string(r(N), "%3.0f")
	count if treatment_group==1 & treat_nap==0 & tag==1
	local N_dc = string(r(N), "%3.0f")
	count if treatment_group==2 & treat_nap==0 & tag==1
	local N_ic = string(r(N), "%3.0f")
	
	count if treatment_group==0 & treat_nap==1 & tag==1
	local N_cn = string(r(N), "%3.0f")
	count if treatment_group==1 & treat_nap==1 & tag==1
	local N_dn = string(r(N), "%3.0f")
	count if treatment_group==2 & treat_nap==1 & tag==1
	local N_in = string(r(N), "%3.0f")

* Joint orthogonality by sub group

	* Demographics
	local varlist "female age no_of_children education"
	
		foreach treatment in treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_j_`treatment'_demo = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Baseline Sleep
	local varlist "bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned"
	
		foreach treatment in treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_j_`treatment'_sleep = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Health, Well Being and Cognition
	local varlist "health_bsl wb_bsl baseline_pvt baseline_hf baseline_corsi cg_bsl pref_bsl"
	
		foreach treatment in treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_j_`treatment'_hwbc = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
	
	* Baseline Work and Savings
	local varlist "baseline_typing baseline_office baseline_prod baseline_earnings bsl_attendance bsl_attendance_2 baseline_savings prior_savings_1000"
	
		foreach treatment in treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_j_`treatment'_work = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}
		
	local varlist "female age no_of_children education bsl_self_report bsl_avg_sleep baseline_bed baseline_sleep_eff no_device_owned health_bsl wb_bsl baseline_pvt baseline_hf baseline_corsi baseline_typing baseline_office baseline_prod baseline_earnings bsl_attendance  baseline_savings prior_savings_1000"
	
		foreach treatment in treat_s_nonap treat_si_nonap treat_c_nap treat_s_nap treat_si_nap { 
		
			reg `treatment' `varlist' if tag==1
			
			local p_val_j_`treatment'_all = string(1 - F(e(df_m), e(df_r), e(F)), "%9.2f")
		
		}



*Exporting

		file open f using "$oa/Tables/TableA1a_balance1_six_index.tex", write replace
				file write f "\begin{tabular}{l*{11}{c}}" _n ///+
				"\toprule" _n ///
				" &\multicolumn{6}{c}{\textbf{Averages}} & \multicolumn{5}{c}{\textbf{$p$-values}} \\" _n ///
				"\cmidrule(lr){2-7}\cmidrule(lr){8-12}  &\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Encouragement}&\multicolumn{1}{c}{Incentives}&\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Encouragement}&\multicolumn{1}{c}{Incentives}&\multicolumn{5}{c}{Test of Equality of:} \\" _n ///
				"&\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{1=2}&\multicolumn{1}{c}{1=3}&\multicolumn{1}{c}{1=4}&\multicolumn{1}{c}{1=5}&\multicolumn{1}{c}{1=6} \\" _n ///
				" & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) \\" _n ///
				"\midrule" _n ///
				"\textit{Panel A. Demographics} & & & & & & & & & \\" _n ///
				"Female & `mean_female_cc' & `mean_female_dc' & `mean_female_ic' & `mean_female_cn' & `mean_female_dn' & `mean_female_in' & `pval_female_dc' & `pval_female_ic' & `pval_female_cn' & `pval_female_dn' & `pval_female_in' \\" _n ///
				" & (`se_female_cc') & (`se_female_dc') & (`se_female_ic') & (`se_female_cn') & (`se_female_dn') & (`se_female_in') & & & & & \\" _n ///
				"Age & `mean_age_cc' & `mean_age_dc' & `mean_age_ic' & `mean_age_cn' & `mean_age_dn' & `mean_age_in' & `pval_age_dc' & `pval_age_ic' & `pval_age_cn' & `pval_age_dn' & `pval_age_in' \\" _n ///
				" & (`se_age_cc') & (`se_age_dc') & (`se_age_ic') & (`se_age_cn') & (`se_age_dn') & (`se_age_in') & & & & & \\" _n ///
				"Number of Children & `mean_no_of_children_cc' & `mean_no_of_children_dc' & `mean_no_of_children_ic' & `mean_no_of_children_cn' & `mean_no_of_children_dn' & `mean_no_of_children_in' & `pval_no_of_children_dc' & `pval_no_of_children_ic' & `pval_no_of_children_cn' & `pval_no_of_children_dn' & `pval_no_of_children_in' \\" _n ///
				" & (`se_no_of_children_cc') & (`se_no_of_children_dc') & (`se_no_of_children_ic') & (`se_no_of_children_cn') & (`se_no_of_children_dn') & (`se_no_of_children_in') & & & & & \\" _n ///
				"Years of Education & `mean_education_cc' & `mean_education_dc' & `mean_education_ic' & `mean_education_cn' & `mean_education_dn' & `mean_education_in' & `pval_education_dc' & `pval_education_ic' & `pval_education_cn' & `pval_education_dn' & `pval_education_in' \\" _n ///
				" & (`se_education_cc') & (`se_education_dc') & (`se_education_ic') & (`se_education_cn') & (`se_education_dn') & (`se_education_in') & & & & & \\" _n ///
				"Familiar with Computer & `mean_computer_comfort_cc' & `mean_computer_comfort_dc' & `mean_computer_comfort_ic' & `mean_computer_comfort_cn' & `mean_computer_comfort_dn' & `mean_computer_comfort_in' & `pval_computer_comfort_dc' & `pval_computer_comfort_ic' & `pval_computer_comfort_cn' & `pval_computer_comfort_dn' & `pval_computer_comfort_in' \\" _n ///
				" & (`se_computer_comfort_cc') & (`se_computer_comfort_dc') & (`se_computer_comfort_ic') & (`se_computer_comfort_cn') & (`se_computer_comfort_dn') & (`se_computer_comfort_in') & & & & & \\" _n ///
				"Unemployed & `mean_unemployed_cc' & `mean_unemployed_dc' & `mean_unemployed_ic' & `mean_unemployed_cn' & `mean_unemployed_dn' & `mean_unemployed_in' & `pval_unemployed_dc' & `pval_unemployed_ic' & `pval_unemployed_cn' & `pval_unemployed_dn' & `pval_unemployed_in' \\" _n ///
				" & (`se_unemployed_cc') & (`se_unemployed_dc') & (`se_unemployed_ic') & (`se_unemployed_cn') & (`se_unemployed_dn') & (`se_unemployed_in') & & & & & \\" _n ///
				"\\" _n ///
				"\textit{Panel B. Baseline Sleep} & & & & & & & & & \\" _n ///
				"Self-Reported Night Sleep (Hrs) & `mean_bsl_self_report_cc' & `mean_bsl_self_report_dc' & `mean_bsl_self_report_ic' & `mean_bsl_self_report_cn' & `mean_bsl_self_report_dn' & `mean_bsl_self_report_in' & `pval_bsl_self_report_dc' & `pval_bsl_self_report_ic' & `pval_bsl_self_report_cn' & `pval_bsl_self_report_dn' & `pval_bsl_self_report_in' \\" _n ///
				" & (`se_bsl_self_report_cc') & (`se_bsl_self_report_dc') & (`se_bsl_self_report_ic') & (`se_bsl_self_report_cn') & (`se_bsl_self_report_dn') & (`se_bsl_self_report_in') & & & & & \\" _n ///
				"Actigraph Night Sleep (Hrs) & `mean_bsl_avg_sleep_cc' & `mean_bsl_avg_sleep_dc' & `mean_bsl_avg_sleep_ic' & `mean_bsl_avg_sleep_cn' & `mean_bsl_avg_sleep_dn' & `mean_bsl_avg_sleep_in' & `pval_bsl_avg_sleep_dc' & `pval_bsl_avg_sleep_ic' & `pval_bsl_avg_sleep_cn' & `pval_bsl_avg_sleep_dn' & `pval_bsl_avg_sleep_in' \\" _n ///
				" & (`se_bsl_avg_sleep_cc') & (`se_bsl_avg_sleep_dc') & (`se_bsl_avg_sleep_ic') & (`se_bsl_avg_sleep_cn') & (`se_bsl_avg_sleep_dn') & (`se_bsl_avg_sleep_in') & & & & & \\" _n ///
				"Actigraph Time in Bed (Hrs) & `mean_baseline_bed_cc' & `mean_baseline_bed_dc' & `mean_baseline_bed_ic' & `mean_baseline_bed_cn' & `mean_baseline_bed_dn' & `mean_baseline_bed_in' & `pval_baseline_bed_dc' & `pval_baseline_bed_ic' & `pval_baseline_bed_cn' & `pval_baseline_bed_dn' & `pval_baseline_bed_in' \\" _n ///
				" & (`se_baseline_bed_cc') & (`se_baseline_bed_dc') & (`se_baseline_bed_ic') & (`se_baseline_bed_cn') & (`se_baseline_bed_dn') & (`se_baseline_bed_in') & & & & & \\" _n ///
				"Sleep Efficiency & `mean_baseline_sleep_eff_cc' & `mean_baseline_sleep_eff_dc' & `mean_baseline_sleep_eff_ic' & `mean_baseline_sleep_eff_cn' & `mean_baseline_sleep_eff_dn' & `mean_baseline_sleep_eff_in' & `pval_baseline_sleep_eff_dc' & `pval_baseline_sleep_eff_ic' & `pval_baseline_sleep_eff_cn' & `pval_baseline_sleep_eff_dn' & `pval_baseline_sleep_eff_in' \\" _n ///
				" & (`se_baseline_sleep_eff_cc') & (`se_baseline_sleep_eff_dc') & (`se_baseline_sleep_eff_ic') & (`se_baseline_sleep_eff_cn') & (`se_baseline_sleep_eff_dn') & (`se_baseline_sleep_eff_in') & & & & & \\" _n ///
				"Number of Sleep Devices Owned & `mean_no_device_owned_cc' & `mean_no_device_owned_dc' & `mean_no_device_owned_ic' & `mean_no_device_owned_cn' & `mean_no_device_owned_dn' & `mean_no_device_owned_in' & `pval_no_device_owned_dc' & `pval_no_device_owned_ic' & `pval_no_device_owned_cn' & `pval_no_device_owned_dn' & `pval_no_device_owned_in' \\" _n ///
				" & (`se_no_device_owned_cc') & (`se_no_device_owned_dc') & (`se_no_device_owned_ic') & (`se_no_device_owned_cn') & (`se_no_device_owned_dn') & (`se_no_device_owned_in') & & & & & \\" _n ///
				"\midrule" _n ///
				"Number of Participants & `N_cc' & `N_dc' & `N_ic' & `N_cn' & `N_dn' & `N_in' & & & & \\" _n ///
				"\bottomrule \\" _n ///
				"\end{tabular}" _n 
		file close f
		

		
		file open f using "$oa/Tables/TableA1b_balance2_six_index.tex", write replace
				file write f "\begin{tabular}{l*{11}{c}}" _n ///+
				"\toprule" _n ///
				" &\multicolumn{6}{c}{\textbf{Averages}} & \multicolumn{5}{c}{\textbf{$p$-values}} \\" _n ///
				" \cmidrule(lr){2-7}\cmidrule(lr){8-12}  &\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Encouragement}&\multicolumn{1}{c}{Incentives}&\multicolumn{1}{c}{Control}&\multicolumn{1}{c}{Encouragement}&\multicolumn{1}{c}{Incentives}&\multicolumn{5}{c}{Test of Equality of:} \\" _n ///
				" &\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{No Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{Nap}&\multicolumn{1}{c}{1 = 2}&\multicolumn{1}{c}{1 = 3}&\multicolumn{1}{c}{1 = 4}&\multicolumn{1}{c}{1 = 5}&\multicolumn{1}{c}{1 = 6} \\" _n ///
				" & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) & (9) & (10) & (11) \\" _n ///
				"\midrule" _n ///
				"\textit{Panel C. Wellbeing, Cognition, Preferences} & & & & & & & & & \\" _n ///
				"Baseline Wellbeing & `mean_wb_bsl_cc' & `mean_wb_bsl_dc' & `mean_wb_bsl_ic' & `mean_wb_bsl_cn' & `mean_wb_bsl_dn' & `mean_wb_bsl_in' & `pval_wb_bsl_dc' & `pval_wb_bsl_ic' & `pval_wb_bsl_cn' & `pval_wb_bsl_dn' & `pval_wb_bsl_in' \\" _n ///
				" & (`se_wb_bsl_cc') & (`se_wb_bsl_dc') & (`se_wb_bsl_ic') & (`se_wb_bsl_cn') & (`se_wb_bsl_dn') & (`se_wb_bsl_in') & & & & & \\" _n ///
				"Baseline Cognition & `mean_cg_bsl_cc' & `mean_cg_bsl_dc' & `mean_cg_bsl_ic' & `mean_cg_bsl_cn' & `mean_cg_bsl_dn' & `mean_cg_bsl_in' & `pval_cg_bsl_dc' & `pval_cg_bsl_ic' & `pval_cg_bsl_cn' & `pval_cg_bsl_dn' & `pval_cg_bsl_in' \\" _n ///
				" & (`se_cg_bsl_cc') & (`se_cg_bsl_dc') & (`se_cg_bsl_ic') & (`se_cg_bsl_cn') & (`se_cg_bsl_dn') & (`se_cg_bsl_in') & & & & & \\" _n ///
				"Baseline Preferences & `mean_pref_bsl_cc' & `mean_pref_bsl_dc' & `mean_pref_bsl_ic' & `mean_pref_bsl_cn' & `mean_pref_bsl_dn' & `mean_pref_bsl_in' & `pval_pref_bsl_dc' & `pval_pref_bsl_ic' & `pval_pref_bsl_cn' & `pval_pref_bsl_dn' & `pval_pref_bsl_in' \\" _n ///
				" & (`se_pref_bsl_cc') & (`se_pref_bsl_dc') & (`se_pref_bsl_ic') & (`se_pref_bsl_cn') & (`se_pref_bsl_dn') & (`se_pref_bsl_in') & & & & & \\" _n ///
				" \\ " _n /// 
				"\textit{Panel D. Baseline Work} & & & & & & & & & \\" _n ///
				"Typing Time (Hrs) & `mean_baseline_typing_cc' & `mean_baseline_typing_dc' & `mean_baseline_typing_ic' & `mean_baseline_typing_cn' & `mean_baseline_typing_dn' & `mean_baseline_typing_in' & `pval_baseline_typing_dc' & `pval_baseline_typing_ic' & `pval_baseline_typing_cn' & `pval_baseline_typing_dn' & `pval_baseline_typing_in' \\" _n ///
				" & (`se_baseline_typing_cc') & (`se_baseline_typing_dc') & (`se_baseline_typing_ic') & (`se_baseline_typing_cn') & (`se_baseline_typing_dn') & (`se_baseline_typing_in') & & & & & \\" _n ///
				"Time in Office (Hrs) & `mean_baseline_office_cc' & `mean_baseline_office_dc' & `mean_baseline_office_ic' & `mean_baseline_office_cn' & `mean_baseline_office_dn' & `mean_baseline_office_in' & `pval_baseline_office_dc' & `pval_baseline_office_ic' & `pval_baseline_office_cn' & `pval_baseline_office_dn' & `pval_baseline_office_in' \\" _n ///
				" & (`se_baseline_office_cc') & (`se_baseline_office_dc') & (`se_baseline_office_ic') & (`se_baseline_office_cn') & (`se_baseline_office_dn') & (`se_baseline_office_in') & & & & & \\" _n ///
				"Productivity & `mean_baseline_prod_cc' & `mean_baseline_prod_dc' & `mean_baseline_prod_ic' & `mean_baseline_prod_cn' & `mean_baseline_prod_dn' & `mean_baseline_prod_in' & `pval_baseline_prod_dc' & `pval_baseline_prod_ic' & `pval_baseline_prod_cn' & `pval_baseline_prod_dn' & `pval_baseline_prod_in' \\" _n ///
				" & (`se_baseline_prod_cc') & (`se_baseline_prod_dc') & (`se_baseline_prod_ic') & (`se_baseline_prod_cn') & (`se_baseline_prod_dn') & (`se_baseline_prod_in') & & & & & \\" _n ///
				"Earnings & `mean_baseline_earnings_cc' & `mean_baseline_earnings_dc' & `mean_baseline_earnings_ic' & `mean_baseline_earnings_cn' & `mean_baseline_earnings_dn' & `mean_baseline_earnings_in' & `pval_baseline_earnings_dc' & `pval_baseline_earnings_ic' & `pval_baseline_earnings_cn' & `pval_baseline_earnings_dn' & `pval_baseline_earnings_in' \\" _n ///
				" & (`se_baseline_earnings_cc') & (`se_baseline_earnings_dc') & (`se_baseline_earnings_ic') & (`se_baseline_earnings_cn') & (`se_baseline_earnings_dn') & (`se_baseline_earnings_in') & & & & & \\" _n ///
				"Attendance & `mean_bsl_attendance_cc' & `mean_bsl_attendance_dc' & `mean_bsl_attendance_ic' & `mean_bsl_attendance_cn' & `mean_bsl_attendance_dn' & `mean_bsl_attendance_in' & `pval_bsl_attendance_dc' & `pval_bsl_attendance_ic' & `pval_bsl_attendance_cn' & `pval_bsl_attendance_dn' & `pval_bsl_attendance_in' \\" _n ///
				" & (`se_bsl_attendance_cc') & (`se_bsl_attendance_dc') & (`se_bsl_attendance_ic') & (`se_bsl_attendance_cn') & (`se_bsl_attendance_dn') & (`se_bsl_attendance_in') & & & & & \\" _n ///
				"\midrule" _n ///
				"Joint Orthogonality Test & & & & & & & `p_val_j_treat_s_nonap_all' & `p_val_j_treat_si_nonap_all' & `p_val_j_treat_c_nap_all' & `p_val_j_treat_s_nap_all' & `p_val_j_treat_si_nap_all' \\" _n ///
				"Number of Participants & `N_cc' & `N_dc' & `N_ic' & `N_cn' & `N_dn' & `N_in' & & & & \\" _n ///
				"\bottomrule \\" _n ///
				"\end{tabular}" _n 
		file close f

